//
//  LHRecommendHotelDetailView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-16.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendHotelDetailView.h"
#import "UIImageView+WebCache.h"

@interface LHRecommendHotelDetailView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIImageView *hotelPic;
@property (nonatomic, retain) UILabel *hotelName;
@property (nonatomic, retain) UIImageView *starPic;
@property (nonatomic, retain) UILabel *hotelAddress;
@property (nonatomic, retain) UILabel *splitLine1;
@property (nonatomic, retain) UILabel *introductionLabel;
@property (nonatomic, retain) UILabel *briefIntroduction;
@property (nonatomic, assign) CGFloat f;
@property (nonatomic, retain) UIScrollView *hotelScrollView;

@end

@implementation LHRecommendHotelDetailView

- (void)dealloc
{
    [_hotelPic release];
    [_hotelName release];
    [_starPic release];
    [_hotelAddress release];
    [_splitLine1 release];
    [_introductionLabel release];
    [_briefIntroduction release];
    [_hotelScrollView release];
    
    [super dealloc];
}

// 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        // 创建scrollView
        self.hotelScrollView = [[UIScrollView alloc] init];
        
        self.hotelScrollView.contentSize = CGSizeMake(0, 800);
        
        _hotelScrollView.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
        
        _hotelScrollView.showsHorizontalScrollIndicator = YES;
        
        _hotelScrollView.delegate =self;
        
        [self addSubview:_hotelScrollView];
        
        [_hotelScrollView release];
        
        // 创建旅店相框
        self.hotelPic = [[UIImageView alloc] init];
        
        [_hotelScrollView addSubview:_hotelPic];
        
        [_hotelPic release];
        
        // 旅店名
        self.hotelName = [[UILabel alloc] init];
        
        [_hotelScrollView addSubview:_hotelName];
        
        [_hotelName setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        [_hotelName release];
        
        // 收藏按钮
        self.collectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
        _collectionButton.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
        
        [_hotelScrollView addSubview:_collectionButton];
        
        // 旅店等级
        self.starPic = [[UIImageView alloc] init];
        
//        _starPic.backgroundColor = [UIColor colorWithRed:0.085 green:0.535 blue:0.862 alpha:1.000];
        
        [_hotelScrollView addSubview:_starPic];
        
        [_starPic release];
        
        // 旅店地址
        self.hotelAddress = [[UILabel alloc] init];
        
        [_hotelScrollView addSubview:_hotelAddress];
        
        [_hotelAddress setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
        
        [_hotelAddress release];
        
        // 分割线1
        self.splitLine1 = [[UILabel alloc] init];
        
        _splitLine1.backgroundColor = [UIColor colorWithRed:0.877 green:0.923 blue:0.967 alpha:1.000];
        
        [_hotelScrollView addSubview:_splitLine1];
        
        [_splitLine1 release];
        
        
        
        // 简介两个字label
        self.introductionLabel = [[UILabel alloc] init];
        
        [_hotelScrollView addSubview:_introductionLabel];
        
        [_introductionLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        [_introductionLabel release];
        
        // 简介
        self.briefIntroduction = [[UILabel alloc] init];
        
        _briefIntroduction.numberOfLines = 0;
        
        [_hotelScrollView addSubview:_briefIntroduction];
        
        [_briefIntroduction setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        [_briefIntroduction release];
        
        
        
        
    }
    
    
    return self;
}

// 设置控件布局信息
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    _hotelPic.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 3);
    
    _hotelName.frame = CGRectMake(10, _hotelPic.frame.origin.y + self.frame.size.height / 3 + 20, 250, 30);
    
    _collectionButton.frame = CGRectMake(self.frame.size.width - 50, _hotelPic.frame.origin.y +self.frame.size.height / 3 + 20, 40, 40);
    _starPic.frame = CGRectMake(10, _hotelName.frame.origin.y + 40, 60, 50);
    
    _hotelAddress.frame = CGRectMake(10, _starPic.frame.origin.y + 60 , self.frame.size.width, 20);
    
    _splitLine1.frame = CGRectMake(0, _hotelAddress.frame.origin.y + 30, self.frame.size.width, 3);
    
    _introductionLabel.frame = CGRectMake(10, _splitLine1.frame.origin.y + 13, 40, 30);
    
    _briefIntroduction.frame = CGRectMake(10, _introductionLabel.frame.origin.y + 50, self.frame.size.width - 20, _f);
    
    _hotelScrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.bounds.size.height);
    
    CGFloat dy = _briefIntroduction.frame.size.height + self.briefIntroduction.frame.origin.y + 200;
    
    
    if (dy < self.bounds.size.height) {
        
        dy = self.bounds.size.height + 20;
    }
    
    [_hotelScrollView setContentSize:CGSizeMake(0, dy)];
    
    
}

- (void)setHotelMo:(LHRecommendHotelModel *)hotelMo
{
    if (_hotelMo != hotelMo) {
        
        [_hotelMo release];
        
        _hotelMo = [hotelMo retain];
        
    }
    
    [_hotelPic sd_setImageWithURL:[NSURL URLWithString:hotelMo.cover] placeholderImage:[UIImage imageNamed:@"word4"]];
    
    _hotelName.text = hotelMo.name;
    
    _hotelAddress.text = hotelMo.address;
    
    _introductionLabel.text = @"简介";
    
    self.f = [[self class] heightWithText:hotelMo.hotelDescription];
    _briefIntroduction.text = hotelMo.hotelDescription;
    
    _starPic.image = [UIImage imageNamed:@"xingji"];
}


// 文字自适应高度
+ (CGFloat)heightWithText:(NSString *)text
{
    
    // 创建一个字典保存文本属性
    // 保存文本文字大小
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    
    // 预设一个尺寸, 文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(LHWidth - 20, 1000);
    
    // 根据文本信息获得一个CGRect
    // 参数1:尺寸范围  参数2:文本属性  参数3:按照什么方式获取Rect  参数4:nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;
    
}

// scrollview拖动时被调用

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    
    
    if (y < 0) {
        
        _hotelPic.frame = CGRectMake(y / 2, y, self.frame.size.width - y, self.frame.size.height / 3 - y);
    }
    
    
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
